#include "netlib.h"
#include "include/pnservice.h"

using namespace pnNetlib;

void
__stdcall
create_service()
{
	pnService::Create();
}

void
__stdcall
destroy_service()
{
	pnService::Destroy();
}

bool
__stdcall
is_service_init()
{
	return pnService::Instance()->is_init();
}

void
__stdcall
initialize_service(int thread_num, int max_session, long buffer_length)
{
	pnService::Instance()->init(thread_num, max_session, buffer_length);
}

void
__stdcall
start_service()
{
	pnService::Instance()->start();
}

void
__stdcall
terminate_service()
{
	pnService::Instance()->terminate();
}

bool 
__stdcall
start_listen(unsigned short port, MsgProc msgProc)
{
	return pnService::Instance()->get_action().start_listen(port, msgProc);
}

pnSessionHandle
__stdcall
start_connect(const char* ip, unsigned short port, MsgProc msgProc)
{
	return pnService::Instance()->get_action().start_new_connection(ip, port, msgProc);
}


void
__stdcall
close_listen()
{
    pnService::Instance()->get_action().close_listen();
}

void 
__stdcall
close_all_session()
{
    pnService::Instance()->get_action().close_all_session();
}

void
__stdcall
close_session(pnSessionHandle session_handle)
{
	pnService::Instance()->get_action().close_session(session_handle);
}

int 
__stdcall
broadcast_msg(unsigned long msg_id, unsigned long msg_len, void* msg_buffer, eUsageType type)
{
	stPnMsgHeader header;
	header.ulMsgLength = msg_len;
	header.ulMsgID = msg_id;

	return pnService::Instance()->get_action().send_msg_to_all(&header, msg_buffer, type);
}

bool
__stdcall
send_msg(pnSessionHandle session_handle, unsigned long msg_id, unsigned long msg_len, void* msg_buffer)
{
	stPnMsgHeader header;
	header.ulMsgLength = msg_len;
	header.ulMsgID = msg_id;

	return pnService::Instance()->get_action().send_msg(session_handle, &header, msg_buffer);
}

int
__stdcall
max_session()
{
	return pnService::Instance()->get_action().max_session();
}

int
__stdcall
cur_listen()
{
	return pnService::Instance()->get_action().get_count(Usage_Listen);
}

int
__stdcall
cur_connect()
{
    return pnService::Instance()->get_action().get_count(Usage_Connect);
}

bool
__stdcall
connect_alive(pnSessionHandle session_handle)
{
	return pnService::Instance()->get_action().is_alive(session_handle);
}

eUsageType
__stdcall
get_usage(int session_handle)
{
    return pnService::Instance()->get_action().get_type(session_handle);
}
